package com.haier.cloudremote.common;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.haier.cloudremote.R;
import com.haier.cloudremote.protocal.CAEProtocal;

public class TroubleShooting {

	private Context context;
	private StringBuffer debugBuffer;
	private String filePath;

	private static int debugReturn;

	public TroubleShooting(Context context, StringBuffer debugBuffer) {
		super();
		this.context = context;
		this.debugBuffer = debugBuffer;
		logSwitch();
	}

	/**
	 * LOG开关
	 * 
	 * @param context
	 * @param debugBuffer
	 */
	public void logSwitch() {
		if (debugBuffer.toString().equals("log")) {
			if (!StaticData.LOG_CONTROL_FLAG) {// debug开关关闭的

				new Thread() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						debugReturn = CAEProtocal.switchOfDebug(1);
						super.run();
					}
				}.start();
				if (debugReturn == 1001) {
					StaticData.LOG_CONTROL_FLAG = true;
//					Toast.makeText(context, "已打开debug开关", Toast.LENGTH_LONG)
//							.show();
				} else {
//					Toast.makeText(context, "打开debug开关失败", Toast.LENGTH_LONG)
//							.show();
				}
			} else {// debug开关为开启的
				new Thread(){
					public void run() {
						 debugReturn = CAEProtocal.switchOfDebug(0);
					};
				}.start();
				if (debugReturn == 1001) {
					StaticData.LOG_CONTROL_FLAG = false;
					SLog.d("ssc", "关闭debug成功");
					new Thread(){
						public void run() {
							filePath = CAEProtocal.getFilePath(0);
						};
					}.start();
					if (filePath != null) {// 读文件
						SLog.d("ssc", "获取到文件路径filePath==" + filePath);
						StringBuffer logBuffer = LogOperation
								.readFileByBytes(filePath);
						new AlertDialog.Builder(context)
								.setTitle("debug信息")
								.setMessage(logBuffer)
								.setPositiveButton("Send email to Haier",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												// TODO Auto-generated method
												// stub
												SendMail sendMail = new SendMail();
												sendMail.send_mail_file(
														"41331189@qq.com",
														"zhangj@haierubic.com",
														"www.haierubic.com",
														"zhangj@haierubic.com",
														"haierubic",
														"ssc remote log",
														context.getResources()
																.getString(
																		R.string.log_emai_text),
														filePath);
											}
										})
								.setNegativeButton("exit",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												// TODO Auto-generated method
												// stub

											}
										}).show();

						// MailOperation.sendMail(filePath);
					} else {
//						Toast.makeText(context, "获取log失败", Toast.LENGTH_LONG)
//								.show();
					}
				} else {
//					Toast.makeText(context, "获取log失败", Toast.LENGTH_LONG)
//							.show();
				}
			}

		}
	}
}
